﻿#include "floor.h"

#include "imagelayer.h"
#include "pubpoint.h"
#include "room.h"

int Floor::m_maxFloorId = 0;

Floor::Floor(QGraphicsItem *parent) : PolygonFeature(parent), m_height(0) {
  m_color = QColor(193, 193, 193, 125);
  m_id = ++m_maxFloorId;
}

Floor::Floor(PolygonFeature &polygon) {
  new (this) Floor();
  copy(polygon);
  m_id = m_maxFloorId++;
}

int Floor::generateId() {
  m_id = m_maxFloorId++;
  return m_id;
}

bool Floor::load(const QJsonObject &jsonObject) {
  PolygonFeature::load(jsonObject);
  m_height = jsonObject["height"].toDouble();
  m_id = jsonObject["_id"].toInt();
  if (m_id > m_maxFloorId) {
    m_maxFloorId = m_id;
  }

  QJsonArray roomArray = jsonObject["funcAreas"].toArray();
  for (int i = 0; i < roomArray.size(); ++i) {
    QJsonObject roomObject = roomArray[i].toObject();

    Room *room = new Room(this);
    if (!room->load(roomObject)) {
      // TODO: show some warning
    }
    room->setParent(this);
  }

  QJsonArray pubArray = jsonObject["pubPoint"].toArray();
  for (int i = 0; i < pubArray.size(); ++i) {
    QJsonObject pubObject = pubArray[i].toObject();

    PubPoint *pubPoint = new PubPoint(this);
    if (!pubPoint->load(pubObject)) {
      // TODO: show some warning
    }
    pubPoint->setParent(this);
  }

  QJsonArray imageArray = jsonObject["imageLayer"].toArray();
  for (int i = 0; i < imageArray.size(); ++i) {
    QJsonObject imageObject = imageArray[i].toObject();
    ImageLayer *imageLayer = new ImageLayer(this);
    if (!imageLayer->load(imageObject)) {
    }
    imageLayer->setParent(this);
  }

  // for GaoDe maps. name is actually brief
  // QString name = jsonObject["name"].toString();
  // if(!name[0].isUpper()){
  //    m_brief = name;
  //}

  return true;
}

bool Floor::save(QJsonObject &jsonObject) const {
  PolygonFeature::save(jsonObject);
  jsonObject["height"] = m_height;
  jsonObject["_id"] = m_id;

  // save the Rooms and pubPoints
  QJsonArray roomArray, pubArray, imageArray;
  foreach (QGraphicsItem *item, this->childItems()) {
    /* Feature inherit from QGraphicsObject,
     * and QGraphicsObject inherit from QObject and QGraphicsItem
     */
    Feature *feature = dynamic_cast<Feature *>(item);
    if (feature == NULL) {
      continue;
    }

    QString className = feature->metaObject()->className();
    if (className == "Room") {
      QJsonObject roomObject;
      static_cast<Room *>(feature)->save(roomObject);
      roomArray.append(roomObject);
    } else if (className == "PubPoint") {
      QJsonObject pubObject;
      static_cast<PubPoint *>(feature)->save(pubObject);
      pubArray.append(pubObject);
    } else if (className == "ImageLayer") {
      QJsonObject imageObject;
      static_cast<ImageLayer *>(feature)->save(imageObject);
      imageArray.append(imageObject);
    }
  }
  jsonObject["funcAreas"] = roomArray;
  jsonObject["pubPoint"] = pubArray;
  jsonObject["imageLayer"] = imageArray;
  return true;
}

double Floor::height() const { return m_height; }

void Floor::setHeight(double height) {
  if (m_height == height) return;
  m_height = height;
  emit heightChanged(m_height);
}

void Floor::resetMaxFloorId() { m_maxFloorId = 0; }

QList<Room *> Floor::getRooms() const {
  QList<Room *> Rooms;
  const QList<QGraphicsItem *> &children = this->childItems();
  QGraphicsItem *item;
  foreach (item, children) {
    Room *room = dynamic_cast<Room *>(item);
    if (room != NULL) {
      Rooms.append(room);
    }
  }
  return Rooms;
}

void Floor::transformFeature(const QMatrix &mat) {
  PolygonFeature::transformFeature(mat);

  const QList<QGraphicsItem *> &children = this->childItems();
  foreach (QGraphicsItem *item, children) {
    Feature *mapFeature = static_cast<Feature *>(item);
    if (mapFeature != NULL) {
      mapFeature->transformFeature(mat);
    }
  }
}
